/**
 * @description 留言管理控制器
 * @author 韩
 */
const { ObjectId } = require("mongodb");
const MESSAGE = require("../../../model/context/message/message-model")
const { getFilter } = require("../../../utils/setFilter");
const dayjs = require("dayjs");

// 创建留言
const addMESSAGE = async ({
    _id = null, userName, msgContext, articleId
}) => {
    const params = {
        createDate: dayjs(),
        userName, msgContext, articleId
    }
    let addRes = null;
    if (_id == null) {
        // 添加创建
        addRes = await MESSAGE.create({
            ...params
        })
    } else {
        // 更新修改
        addRes = await MESSAGE.findOneAndUpdate({ _id: _id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const { _id, userName, msgContext, articleId } = addRes

        // deleteProductionImage({ imgList: discardImage })
        return {
            code: 0,
            msg: "成功",
            data: {
                _id, userName, msgContext, articleId
            }
        }
    }
    return {
        code: 12,
        msg: "失败",
    }
}

// 查询列表 
const queryMESSAGE = async ({ userName = null, _id = null, articleId = null, current, pageSize }) => {

    try {
        let filter = {
            _id: _id && { $eq: _id },//_id精确匹配
            userName: userName && { $regex: eval("/" + userName + "/i") },//商品名模糊查询
            articleId: articleId && { $regex: eval("/" + articleId + "/i") },//文章id查询
        }
        filter = getFilter(filter)
        console.log("新的查询条件：", filter)
        const list = await MESSAGE.find(filter).sort({ createDate: -1 }).skip((current - 1) * pageSize).limit(pageSize).exec()
        const totalCount = await MESSAGE.find(filter).countDocuments()
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        console.log(error)
        return {
            code: 1,
            msg: "查询失败"
        }
    }
}

// 根据id查详情
const getMESSAGEDetail = async ({ _id }) => {
    try {
        const res = await MESSAGE.findOne({
            _id
        })

        if (res) {
            const { _id, userName, msgContext, articleId, createDate } = res;
            return {
                code: 0,
                msg: "查询成功",
                data: {
                    _id: _id, userName, msgContext, articleId, createDate
                }
            }
        } else {
            return {
                code: '0001',
                msg: "留言详情获取失败"
            }
        }
    } catch (error) {
        console.log(error)
        return {
            code: 1,
            msg: "查询失败"
        }
    }

}

// 删除
const deltetMESSAGE = async ({ ids = [] }) => {
    try {
        const deleteResult = await MESSAGE.deleteMany({
            _id: { $in: ids }
        });
        if (deleteResult.deletedCount) {
            return {
                code: 0,
                msg: `删除了${deleteResult.deletedCount}条数据`
            }
        } else {
            return {
                code: '0010',
                msg: `删除失败`
            }
        }
    } catch (error) {
        return {
            code: '0010',
            msg: `删除失败`
        }
    }


}
module.exports = {
    addMESSAGE, queryMESSAGE, getMESSAGEDetail, deltetMESSAGE
}